package com.wwwarehouse.resource_center.fragment.createobject.modifydelete;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.adapter.pop.BubblePopAdapter;
import com.wwwarehouse.common.bean.filter.FilterBean;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.ModifyDeleteBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.eventbus_event.GoodsEvent;
import com.wwwarehouse.resource_center.fragment.base.MyBaseViewPagerFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/10/20.
 *         <p/>
 *         物和资源修改,删除统一入口
 *         <p>
 *         物的分类：H_GOODS(商品)、H_TOOLS(工具)、H_SHOP(实体店)、H_STOCK(仓库)、
 *         资源的分类：H_SALE(供货)、H_STORE(仓储)、H_LABOUR(劳务)、H_PROCESS(加工)、H_SELL(销售)、H_TRANSPORT(运输)
 *          UPDATE_H_GOODS	 修改商品
            UPDATE_H_TOOLS	 修改工具
            UPDATE_H_SHOP	 修改实体店
            UPDATE_H_STOCK	 修改仓库
            DELETE_H_GOODS	 删除商品
            DELETE_H_TOOLS	 删除工具
            DELETE_H_SHOP	 删除实体店
            DELETE_H_STOCK	 删除仓库
            UPDATE_RESOURCES 修改资源
            DELETE_RESOURCES 删除资源
 *
 */
@Route(path = ResourceConstant.PATH_GOODS_AND_RES_MODIFY_DELETE)
public class GoodsAndResModifyDeleteViewPagerFragment extends MyBaseViewPagerFragment {

    int mNum = 7;
    public static final int PAGINATION_ITEMORRESOURCE = 1;  //
    private String businessId = "";          //业务单位ID
    private  String   mSort ="update_time desc" ;
    private List<FilterBean> sortList; //排序
    private String  taskType   = "";
    private String  type   = "";
    private String  mSeachName ="";
    private boolean isDelete = false;  //  false修改  true删除
    private String  uRl;
    @Override
    protected void loadDatas() {
        EventBus.getDefault().register(this);
        Bundle bundle = getArguments();
        mCustomScroll.setOnPullListener(null);
        if (null!=bundle){
            CardDeskFunctionResponseBean.TaskBean taskBean = (CardDeskFunctionResponseBean.TaskBean) bundle.getSerializable(Constant.KEY_CARD_DESK_FUNCTION_DETAILS);
            businessId = taskBean.getBusinessId();
            taskType = taskBean.getTaskType();
        }
        SettingTitle();
        sp.putValue("businessId",businessId);
        sortList = new ArrayList<>();
        sortList.add(new FilterBean(true, "更新时间由近到远")); //默认排序
        sortList.add(new FilterBean(false,"更新时间由远到近"));
    }

    private void SettingTitle() {
        switch (taskType){
            case "UPDATE_H_SHOP" :
                type = "H_SHOP";
                isDelete = false;
                mActivity.setTitle(getString(R.string.modify_shop));
                break;
            case "UPDATE_H_STOCK":
                isDelete = false;
                type = "H_STOCK";
                mActivity.setTitle(getString(R.string.modify_stock));
                break;
            case "UPDATE_H_GOODS" :
                isDelete = false;
                type = "H_GOODS";
                mActivity.setTitle(getString(R.string.modify_goods));
                break;
            case "UPDATE_H_TOOLS" :
                isDelete = false;
                type = "H_TOOLS";
                mActivity.setTitle(getString(R.string.modify_tool));
                break;
            case "UPDATE_RESOURCES" :
                isDelete = false;
                type = "";
                mActivity.setTitle(getString(R.string.modify_res));
                break;
            case "DELETE_H_GOODS" :
                isDelete = true;
                type = "H_GOODS";
                mActivity.setTitle(getString(R.string.delete_goods));
                break;
            case "DELETE_H_TOOLS" :
                isDelete = true;
                type = "H_TOOLS";
                mActivity.setTitle(getString(R.string.delete_tool));
                break;
            case "DELETE_H_SHOP" :
                isDelete = true;
                type = "H_SHOP";
                mActivity.setTitle(getString(R.string.delete_shop));
                break;
            case "DELETE_H_STOCK" :
                isDelete = true;
                type = "H_STOCK";
                mActivity.setTitle(getString(R.string.delete_stock));
                break;
            case "DELETE_RESOURCES" :
                isDelete = true;
                type = "";
                mActivity.setTitle(getString(R.string.delete_res));
                break;
            default:
                break;
        }
    }

    @Override
    public void requestDatas() {
        pagination(mSeachName,mSort);
    }

    private void pagination(String mSeachName, String mSort) {
        Map map = new HashMap();
        Map<String, Object> query = new HashMap<>();
//        map.put("businessId", sp.getValue("businessId"));     //新接口文档没有
//        map.put("itemType", itemType);//按分类查询物或资源，非必须
//        map.put("currentStatus","");     //上下架（非必须） "0"无效   "1"下架  "2"上架
        map.put("name", mSeachName);                 //按名称模糊查询，非必须
        if (!TextUtils.isEmpty(type)){   //查询物
            map.put("type", type);
            uRl = ResourceConstant.SELECT_BYNAME ;
        }else {
            // 资源 type 不传查询资源
            uRl = ResourceConstant.QUZZY_QUERY_DEFINED_AND_RELATION;
        }
        map.put("ownerUkid",businessId);
//        map.put("currentStatus","");
        map.put("page", 1);
        map.put("size", mNum);
        map.put("sort", mSort);//update_time asc
//        map.put("query", query);
        httpPost(uRl, map, PAGINATION_ITEMORRESOURCE, false, "");
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what) {
            case PAGINATION_ITEMORRESOURCE:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    ModifyDeleteBean goodsSelectLabelBean = JSON.parseObject(commonClass.getData().toString(), ModifyDeleteBean.class);
                    int total = goodsSelectLabelBean.getTotal();
                    if (total<=0){
                        mLoadingView.setVisibility(View.VISIBLE);
                        mLoadingView.showEmptyView(true);
                        mLoadingView.setEmptyListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mLoadingView.showProgressView(true);
                                pagination(mSeachName, mSort);
                            }
                        });
                        return;
                    }
                    mCustomScroll.setOnPullListener(this);
                    Bundle bundle = new Bundle();
                    bundle.putString("type", type);   //type
                    bundle.putString("ownerUkid", businessId);
                    bundle.putString("search", mSeachName);
                    bundle.putString("sort", mSort);
                    bundle.putString("URL", uRl);
                    bundle.putBoolean("isDelete", isDelete);
                    SetFragmentAndPage(total, mNum,new GoodsAndResModifyDeleteListFragment().getClass().getName(),bundle);
                }
                break;
        }
    }

    @Override
    public void onReLoad(int code) {
        pagination(mSeachName, mSort);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof GoodsAndResModifyDeleteViewPagerFragment){
            if (!TextUtils.isEmpty(type)){
                SettingTitle();
            }
        }
    }

    @Override
    public void searchNoRealTimeClick(String text) {
        //搜索
        if (peekFragment() instanceof GoodsAndResModifyDeleteViewPagerFragment ) {
            mSeachName = text;
            pagination(mSeachName, mSort);
            Log.d("searchNoRealTimeClick",mSeachName);
        }
    }

    @Override
    public void cancelClick() {
        if (peekFragment()instanceof GoodsAndResModifyDeleteViewPagerFragment){
            mSeachName ="";
            pagination(mSeachName, mSort);
        }
    }

    @Override
    public void sortClick(View anchor) {
        //排序
        if (peekFragment() instanceof GoodsAndResModifyDeleteViewPagerFragment ){
            PopUpUtils.showBubbleDown(anchor, mActivity, sortList, new BubblePopAdapter.OnPopListClickListener() {
                @Override
                public void setOnItemClickListener(int i, View view) {
                    switch (i) {
                        case 0:
                            mSort = "update_time desc";      //时间正序 update_time desc
                            sortList.get(0).setSelect(true);
                            sortList.get(1).setSelect(false);
                            break;
                        case 1:
                            mSort = "update_time asc";        //时间倒序
                            sortList.get(0).setSelect(false);
                            sortList.get(1).setSelect(true);
                            break;
                        default:
                            break;
                    }
                    //接口请求
                    pagination(mSeachName,mSort);
                }
            });
        }
    }
    public void onEventMainThread(GoodsEvent event) {
        switch (event.getCode()) {
            case GoodsEvent.GOODS_DELETE:
                pagination("", mSort);
                break;
        }
    }
    @Override
    public void onDownPull() {
        if (isShow){
            ((BaseCardDeskActivity) mActivity).hideFilterBt();
            ((BaseCardDeskActivity)mActivity).showSearchTitle();
        }else {
            ((BaseCardDeskActivity)mActivity).hideSearchTitle();
        }
        isShow = !isShow;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ((BaseCardDeskActivity) mActivity).showFilterBt();
    }
}
